﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;

namespace Nirvana
{
    public static class GeometryUtilityHelper
    {
        private static readonly Action<Plane[], Matrix4x4> action;

        static GeometryUtilityHelper()
        {
            MethodInfo method = typeof(GeometryUtility).GetMethod("Internal_ExtractPlanes", BindingFlags.Static | BindingFlags.NonPublic);
            if (method != null)
            {
                action = (Action<Plane[], Matrix4x4>)Delegate.CreateDelegate(typeof(Action<Plane[], Matrix4x4>), method, false);
            }
        }


        public static void ExtractPlanes(Plane[] planes, Camera camera)
        {
            Matrix4x4 worldToProjectionMatrix = camera.projectionMatrix * camera.worldToCameraMatrix;
            GeometryUtilityHelper.ExtractPlanes(planes, worldToProjectionMatrix);
        }
        public static void ExtractPlanes(Plane[] planes, Matrix4x4 worldToProjectionMatrix)
        {
            if (action != null)
            {
                action(planes, worldToProjectionMatrix);
            }
            else
            {
                Plane[] sourceArray = GeometryUtility.CalculateFrustumPlanes(worldToProjectionMatrix);
                Array.Copy(sourceArray, planes, 6);
            }
        }
    }
}

